#####################################
# Cleaning up Politics: Main Results  
#####################################

rm(list=ls())

# load packages 
library(foreign)
library(lmtest)
library(sandwich)
library(cregg)
library(ggplot2)
library(gridExtra)

# load data
load("anticorr_replication_data.RData")


########################
##### Figure 1 #########
########################

# AMCE
amces1 <- cj(d, outcome_q1 ~ anticorruption + gender + ideology + party + bribery + embezzlement + competence + age + education, id = ~id)

# Plot
plot1 <- plot(amces1[1:3, ], text.size = 17, xlab = "Change in Electoral Support") +
  theme_classic() +
  ggtitle("") +
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "none",
    panel.border = element_rect(linetype = "solid", color = "black", fill = NA),
    panel.background = element_rect(fill = "white"),
    axis.line = element_blank()
  ) +
  scale_color_manual(values = rep("black", 9))

plot1

ggsave("figure1.png", plot1, width = 7, height = 3, units = "in", dpi = 600)



######################
###### Figure 2 ######
######################

# AMCE
amces2 <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + bribery + embezzlement + competence + age + education, id = ~id)

# Plot
plot2 <- plot(amces2[1:3, ], text.size = 17, xlab = "Change in Perceived Effectiveness in Reducing Corruption") +
  theme_classic() +
  ggtitle("") +
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "none",
    panel.border = element_rect(linetype = "solid", color = "black", fill = NA),
    panel.background = element_rect(fill = "white"),
    axis.line = element_blank()
  ) +
  scale_color_manual(values = rep("black", 9))

plot2

ggsave("figure2.png", plot2, width = 7, height = 3, units = "in", dpi = 600)

##########################
######## Figure 3 ########
##########################

# AMCE
amces3 <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "amce", by = ~bribery)
amces3 <- rbind(amces3[1:3,], amces3[21:23,]) 
diff_amces3 <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "amce_diff", by = ~bribery)
diff_amces3 <- diff_amces3[3:4, ]

# Plot AMCE by bribery
plot3 <- plot(amces3, group = "bribery", vline = 0.0) + theme(legend.title = element_blank()) + scale_colour_manual(values=c("black", "grey"), na.translate = F)
plot3

# Conditional marginal means
mms3 <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "mm", by = ~bribery)
mms3 <- rbind(mms3[1:3,], mms3[21:23,]) 
p3<-plot(mms3, group = "bribery", vline = 0.5) + theme(legend.title = element_blank()) + scale_colour_manual(values=c("black", "grey"), na.translate = F)
p3

# Differences in MM
diff_mms3 <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "mm_diff", by = ~bribery)
diff_mms3 <- rbind(diff_mms3[1:3,]) 

# Combine AMCE and MM 
f3 <- grid.arrange(plot3, p3, ncol = 2)
ggsave("figure3.png", f3, width = 9, height = 3, units = "in", dpi = 600)

##########################
######## Figure 4 ########
##########################

# AMCE by bribery
amces4 <- cj(d, outcome_q1 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "amce", by = ~bribery)
amces4 <- rbind(amces4[1:3,], amces4[21:23,]) 

# Plot AMCE by bribery
plot4 <- plot(amces4, group = "bribery", vline = 0.0) + theme(legend.title = element_blank()) + scale_colour_manual(values=c("black", "grey"), na.translate = F) 
plot4

# Conditional marginal means
mms4 <- cj(d, outcome_q1 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "mm", by = ~bribery)
mms4 <- rbind(mms4[1:3,], mms4[21:23,]) 
p4<-plot(mms4, group = "bribery", vline = 0.5) + theme(legend.title = element_blank())+ scale_colour_manual(values=c("black", "grey"), na.translate = F) 
p4

# Differences in MMs
diff_mms4 <- cj(d, outcome_q1 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "mm_diff", by = ~bribery)
diff_mms4 <- rbind(diff_mms4[1:3,]) 

# Combine AMCE and MM 
f4 <- grid.arrange(plot4, p4, ncol = 2)
ggsave("figure4.png", f4, width = 9, height = 3, units = "in", dpi = 600)

#################################
########### Figure 5 ############
#################################

# AMCE by gender
amces5 <- cj(d, outcome_q4 ~ anticorruption + ideology + party + bribery + embezzlement + competence + age + education, id = ~id, estimate = "amce", by = ~gender)
amces5 <- rbind(amces5[1:3,], amces5[21:23,])

# Plot AMCE by gender
plot5 <- plot(amces5, group = "gender", vline = 0.0) + theme(legend.title = element_blank()) + scale_colour_manual(values=c("black", "grey"), na.translate = F)
plot5

# Conditional MM
mms5 <- cj(d, outcome_q4 ~ anticorruption + ideology + party + bribery + embezzlement + competence + age + education, id = ~id, estimate = "mm", by = ~gender)
mms5 <- rbind(mms5[1:3,], mms5[21:23,]) # selecting rows of interest
p5<-plot(mms5, group = "gender", vline = 0.5) + theme(legend.title = element_blank())+ scale_colour_manual(values=c("black", "grey"), na.translate = F) 
p5

# Combine AMCE and MM 
f5 <- grid.arrange(plot5, p5, ncol = 2)
ggsave("figure5.png", f5, width = 9, height = 3, units = "in", dpi = 600)

#################################
########### Figure 6 ############
#################################

# AMCE by gender
amces6 <- cj(d, outcome_q1 ~ anticorruption + ideology + party + bribery + embezzlement + competence + age + education, id = ~id, estimate = "amce", by = ~gender)
amces6 <- rbind(amces6[1:3,], amces6[21:23,])

# Plot AMCE by gender
plot6 <- plot(amces6, group = "gender", vline = 0.0) + theme(legend.title = element_blank()) + scale_colour_manual(values=c("black", "grey"), na.translate = F)
plot6

# Conditional MM
mms6 <- cj(d, outcome_q1 ~ anticorruption + ideology + party + bribery + embezzlement + competence + age + education, id = ~id, estimate = "mm", by = ~gender)
mms6 <- rbind(mms6[1:3,], mms6[21:23,]) # selecting rows of interest
p6<-plot(mms6, group = "gender", vline = 0.5) + theme(legend.title = element_blank())+ scale_colour_manual(values=c("black", "grey"), na.translate = F) 
p6

# Combine AMCE and MM 
f6 <- grid.arrange(plot6, p6, ncol = 2)
ggsave("figure6.png", f6, width = 9, height = 3, units = "in", dpi = 600)

#######################################
######## Figure A8 in Appendix ######## 
#######################################

# AMCE by embezzlement
amcesB <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + bribery + competence + age + education, id = ~id, estimate = "amce", by = ~embezzlement)
diff_amcesB <- cj(d, outcome_q4 ~ anticorruption + gender + ideology + party + bribery + competence + age + education, id = ~id, estimate = "amce_diff", by = ~embezzlement)
plotB = plot(rbind(amcesB, diff_amcesB)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("Change in Anti-Corruption Commitment Perception") + theme(plot.title = element_text(hjust = 0.5))

# modifying and saving
plotB <- plotB + 
  scale_color_manual(values = rep("black", 9))+
  theme(legend.position = "none")
plotB

#######################################
######## Figure A9 in Appendix ######## 
#######################################

# AMCE by embezzlement
amcesA <- cj(d, outcome_q1 ~ anticorruption + gender + ideology + party + bribery + competence + age + education, id = ~id, estimate = "amce", by = ~embezzlement)
diff_amcesA <- cj(d, outcome_q1 ~ anticorruption + gender + ideology + party + bribery + competence + age + education, id = ~id, estimate = "amce_diff", by = ~embezzlement)
plotA = plot(rbind(amcesA, diff_amcesA)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("Change in Electoral Support") + theme(plot.title = element_text(hjust = 0.5))
plotA

# modifying and saving plot
plotA <- plotA + 
  scale_color_manual(values = rep("black", 9))+
  theme(legend.position = "none")
plotA
